package com.zzh.lib.views.touch;


import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.RelativeLayout;

import com.zzh.lib.views.R;

/**
 * Created by ZZH on 2022/11/23.
 *
 * @Date: 2022/11/23
 * @Email: zzh_hz@126.com
 * @QQ: 1299234582
 * @Author: zzh
 * @Description: 拦截和分发触摸事件
 */
public class HTouchLayout extends RelativeLayout {
    private boolean dispatchTouchEvent, interceptTouchEvent;

    public HTouchLayout(Context context) {
        this(context, null);
    }

    public HTouchLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        initAttrs(context, attrs);
    }

    public HTouchLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initAttrs(context, attrs);
    }

    private void initAttrs(Context context, AttributeSet attrs) {
        if (attrs != null) {
            TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.HTouchLayout);
            dispatchTouchEvent = ta.getBoolean(R.styleable.HTouchLayout_zh_touch_dispatch, false);
            interceptTouchEvent = ta.getBoolean(R.styleable.HTouchLayout_zh_touch_intercept, false);
            ta.recycle();
        }
    }

    public HTouchLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        initAttrs(context, attrs);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        return dispatchTouchEvent;
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        return interceptTouchEvent;
    }
}
